package com.quanta.anag.candidati.dynamicjsf;

import com.icesoft.faces.component.ext.UIColumn;
import java.util.TimeZone;
import javax.el.ValueExpression;
import javax.faces.component.html.HtmlOutputText;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.DateTimeConverter;

/**
 *
 * @author roberto
 */
public class JsfDataTableColumn {

    private String styleClass;
    private String header;
    private String value;
    private JsfDateConverter jsfDateConverter;

    public JsfDataTableColumn() {
    }

    public JsfDataTableColumn(String styleClass, String header, String value, JsfDateConverter jsfDateConverter) {
        this.styleClass = styleClass;
        this.header = header;
        this.value = value;
        this.jsfDateConverter = jsfDateConverter;
    }
    public JsfDataTableColumn(String styleClass, String header, String value) {
        this.styleClass = styleClass;
        this.header = header;
        this.value = value;
    }

    public void setStyleClass(String styleClass) {
        this.styleClass = styleClass;
    }

    public void setHeader(String header) {
        this.header = header;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public void setJsfDateConverter(JsfDateConverter jsfDateConverter) {
        this.jsfDateConverter = jsfDateConverter;
    }
    

    public UIColumn createUIColumn() {
        UIColumn retval = new UIColumn();

        if (styleClass != null && styleClass.length() > 0) {
            if (styleClass.contains("#{")) {
                retval.setValueExpression("styleClass", createValueExpression(styleClass, String.class));
            } else {
                retval.setStyleClass(styleClass);
            }
        }

        HtmlOutputText outHeader = new HtmlOutputText();
        if (header != null && header.length() > 0) {
            if (header.contains("#{")) {
                outHeader.setValueExpression("value", createValueExpression(header, String.class));
            } else {
                outHeader.setValue(header);
            }
        }
        retval.setHeader(outHeader);

        HtmlOutputText outValue = new HtmlOutputText();
        if (value != null && value.length() > 0) {
            if (value.contains("#{")) {
                outValue.setValueExpression("value", createValueExpression(value, String.class));
            } else {
                outValue.setValue(value);
            }
            if( jsfDateConverter != null){
                outValue.setConverter(jsfDateConverter.createConverter());
            }
            if( value.contains("settore")){
                outValue.setValueExpression("converter", createValueExpression("#{SessionBean.candidati.lookupCandidatoSettoreItemsAvailableSelectOneConverter}", Converter.class));
            }
        }
        retval.getChildren().add(outValue);
        return retval;

    }

    private ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        return facesContext.getApplication().getExpressionFactory().createValueExpression(
                facesContext.getELContext(), valueExpression, valueType);
    }
}
